package com.frkj.yuan.fragment.home;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;

import com.alipay.sdk.app.PayTask;
import com.frkj.yuan.R;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.view.PayResult;

import java.util.Map;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/5/3 17 ：03
 * 项目名称：yuanban
 */
public class PayActivity extends BaseActivity {
    private static final int SDK_PAY_FLAG = 1;
    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_view;
    }

    @Override
    public void initLayoutView() {
        String payMsg = "alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_cert_sn=0a481e2f5482616a0a53469ba5f3445c&app_id=2021003126666090&biz_content=%7B%22body%22%3A%22%E6%99%AE%E9%80%9A%E9%99%AA%E8%AF%8A%22%2C%22out_trade_no%22%3A%22A452710010098%22%2C%22subject%22%3A%22%E6%99%AE%E9%80%9A%E9%99%AA%E8%AF%8A%E8%B4%B9%E7%94%A8%E6%94%AF%E4%BB%98%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F120.27.203.113%3A8080%2FaliPay%2Fnotify&sign=cVdCkh9tYzx0EDJR3X3f74ofkzrLGZxX2E1LC7XD3FgUra%2Fcu%2BzijFgsEQVl7COza2igPMq29EO%2BgWL0kV7ogs2Vhvab%2BFd0kQjzTEIdMuMT8p4TXOMN0OVpmURpwgY%2BTCG5ELIus1Q4E7MIkl%2BT6I4nHstyTEQX%2Fgbgq3GnfBWUGUhafIae4vw2bxNFqvGi3eyZKVu2OeKz7bbmmwHybog7%2F5ma8nimksVCIPjg2VnayVj14WSZPX6rNJyKgqZYJBurdamSzEdGXuXh%2BBgF2fTMFb%2BuBOLoJ0oKQX0MVLpHWEs6AULpoTkSCQzHBDdKMgg9NXT9xBSWunz60SL7vA%3D%3D&sign_type=RSA2&timestamp=2022-05-05+14%3A47%3A07&version=1.0";
        findViewById(R.id.zhifu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Runnable payRunnable = new Runnable() {
                    @Override
                    public void run() {
                        // 构造PayTask 对象
                        PayTask alipay = new PayTask(PayActivity.this);
                        // 调用支付接口，获取支付结果
                        String result = alipay.pay(payMsg, true);
                        Log.e("result09", result);
                        Message msg = new Message();
                        msg.what = SDK_PAY_FLAG;
                        msg.obj = result;
                        Log.e("result", result);
                        mHandler.sendMessage(msg);
                    }
                };
                Thread payThread = new Thread(payRunnable);
                payThread.start();
            }
        });
    }
    private Handler mHandler = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    break;
                }
            }

        }
    };
}
